﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Human
{
	public class FrmCapnhat : System.Windows.Forms.Form
	{
        LibHuman.AccessData mDB = new LibHuman.AccessData();
        Language lan = new Language();
		private string s_thumuc="",user="";
		private DataSet ds_k,ds_m;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.DataGrid dg;
		private System.Windows.Forms.CheckedListBox medisoft_kp;
		private PinkieControls.ButtonXP butCapnhat;
		private PinkieControls.ButtonXP butKetthuc;
		
		private System.ComponentModel.Container components = null;

		public FrmCapnhat()
		{
			InitializeComponent();
            lan.Read_Language_to_Xml(this.Name.ToString(), this);
            lan.Changelanguage_to_English(this.Name.ToString(), this);
		}
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmCapnhat));
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.medisoft_kp = new System.Windows.Forms.CheckedListBox();
            this.dg = new System.Windows.Forms.DataGrid();
            this.butCapnhat = new PinkieControls.ButtonXP();
            this.butKetthuc = new PinkieControls.ButtonXP();
            ((System.ComponentModel.ISupportInitialize)(this.dg)).BeginInit();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.ForeColor = System.Drawing.Color.DarkBlue;
            this.label1.Location = new System.Drawing.Point(5, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(390, 23);
            this.label1.TabIndex = 0;
            this.label1.Text = "Khoa phòng ( Nhân sự)";
            this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // label2
            // 
            this.label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label2.ForeColor = System.Drawing.Color.DarkBlue;
            this.label2.Location = new System.Drawing.Point(400, 15);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(385, 23);
            this.label2.TabIndex = 1;
            this.label2.Text = "Khoa phòng ( Medisoft )";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // medisoft_kp
            // 
            this.medisoft_kp.CheckOnClick = true;
            this.medisoft_kp.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.medisoft_kp.Location = new System.Drawing.Point(400, 40);
            this.medisoft_kp.Name = "medisoft_kp";
            this.medisoft_kp.Size = new System.Drawing.Size(385, 468);
            this.medisoft_kp.TabIndex = 3;
            this.medisoft_kp.SelectedIndexChanged += new System.EventHandler(this.medisoft_kp_SelectedIndexChanged);
            // 
            // dg
            // 
            this.dg.BackgroundColor = System.Drawing.SystemColors.Control;
            this.dg.CaptionBackColor = System.Drawing.SystemColors.Control;
            this.dg.CaptionVisible = false;
            this.dg.DataMember = "";
            this.dg.FlatMode = true;
            this.dg.HeaderForeColor = System.Drawing.SystemColors.ControlText;
            this.dg.Location = new System.Drawing.Point(5, 40);
            this.dg.Name = "dg";
            this.dg.RowHeaderWidth = 10;
            this.dg.Size = new System.Drawing.Size(390, 468);
            this.dg.TabIndex = 6;
            this.dg.CurrentCellChanged += new System.EventHandler(this.dg_CurrentCellChanged);
            // 
            // butCapnhat
            // 
            this.butCapnhat.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
            this.butCapnhat.Cursor = System.Windows.Forms.Cursors.Hand;
            this.butCapnhat.DefaultScheme = true;
            this.butCapnhat.DialogResult = System.Windows.Forms.DialogResult.None;
            this.butCapnhat.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butCapnhat.ForeColor = System.Drawing.Color.DarkBlue;
            this.butCapnhat.Hint = "";
            this.butCapnhat.Image = global::Human.Properties.Resources.save;
            this.butCapnhat.Location = new System.Drawing.Point(305, 515);
            this.butCapnhat.Name = "butCapnhat";
            this.butCapnhat.Scheme = PinkieControls.ButtonXP.Schemes.Blue;
            this.butCapnhat.Size = new System.Drawing.Size(90, 28);
            this.butCapnhat.TabIndex = 30;
            this.butCapnhat.Text = "&Cập nhất";
            this.butCapnhat.TextColor = System.Drawing.Color.DarkBlue;
            this.butCapnhat.Click += new System.EventHandler(this.butCapnhat_Click);
            // 
            // butKetthuc
            // 
            this.butKetthuc.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))), ((int)(((byte)(240)))));
            this.butKetthuc.Cursor = System.Windows.Forms.Cursors.Hand;
            this.butKetthuc.DefaultScheme = true;
            this.butKetthuc.DialogResult = System.Windows.Forms.DialogResult.None;
            this.butKetthuc.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butKetthuc.ForeColor = System.Drawing.Color.DarkBlue;
            this.butKetthuc.Hint = "";
            this.butKetthuc.Image = global::Human.Properties.Resources.close_r;
            this.butKetthuc.Location = new System.Drawing.Point(401, 515);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Scheme = PinkieControls.ButtonXP.Schemes.Blue;
            this.butKetthuc.Size = new System.Drawing.Size(85, 28);
            this.butKetthuc.TabIndex = 35;
            this.butKetthuc.Text = "&Kết thúc";
            this.butKetthuc.TextColor = System.Drawing.Color.DarkBlue;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // FrmCapnhat
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(792, 566);
            this.Controls.Add(this.butKetthuc);
            this.Controls.Add(this.butCapnhat);
            this.Controls.Add(this.dg);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.medisoft_kp);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "FrmCapnhat";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Cập nhật khoa phòng";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dg)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion		
		[STAThread]

		private void Form1_Load(object sender, System.EventArgs e)
		{
            user = mDB.user;
			try
			{
				DataSet dsxml=new DataSet();
				dsxml.ReadXml("duongdan.xml");
				s_thumuc=dsxml.Tables[0].Rows[0][0].ToString();
			}
			catch
			{
			}
			f_load_ketoan_kp();		
			f_load_Medisoft_kp();
		}
		private void f_load_ketoan_kp()
		{
            ds_k = mDB.get_data(" select * from "+user+".bophan");
			dg.DataSource=ds_k.Tables[0];
			Addgrid(ds_k);
		}
		private void f_load_Medisoft_kp()
		{
			ds_m=mDB.get_data("select * from medibv.dm_01 where ma not in (1,2,3) and stt not in('A','B','C','D') order by ma");
			foreach(DataRow r in ds_m.Tables[0].Rows)
			{
				medisoft_kp.Items.Add(r["TEN"].ToString());
			}
		}

		#region Addgrid
		private void Addgrid(DataSet ds)
		{
			DataGridTableStyle dtt = new DataGridTableStyle();
			DataGridTextBoxColumn c;
			dg.TableStyles.Clear();
			dtt.MappingName = ds.Tables[0].TableName;
			dtt.AlternatingBackColor = Color.Beige;
			dtt.BackColor = Color.GhostWhite;
			dtt.ForeColor = Color.MidnightBlue;
			dtt.GridLineColor = Color.RoyalBlue;
			dtt.HeaderBackColor = Color.MidnightBlue;
			dtt.HeaderForeColor = Color.Lavender;
			dtt.SelectionBackColor = Color.Teal;
			dtt.SelectionForeColor = Color.PaleGreen;
			dtt.RowHeaderWidth = 10;

			c = new DataGridTextBoxColumn();
			c.MappingName = "madv";
			c.Width = 60;
			c.HeaderText = "Mã đơn vị";
			dtt.GridColumnStyles.Add(c);
			dg.TableStyles.Add(dtt);

			c = new DataGridTextBoxColumn();
			c.MappingName = "tenbp";
			c.Width =230;
			c.HeaderText = "Tên bộ phận";
			dtt.GridColumnStyles.Add(c);
			dg.TableStyles.Add(dtt);

			c = new DataGridTextBoxColumn();
			c.MappingName = "mabaocao";
			c.Width = 100;
			c.ReadOnly = true;
			c.HeaderText = "MAKP báo cáo";
			dtt.GridColumnStyles.Add(c);
			dg.TableStyles.Add(dtt);
			
		}
        #endregion
	
		private void dg_CurrentCellChanged(object sender, System.EventArgs e)
		{
			try
			{
				dg.UnSelect(dg.CurrentRowIndex);
			}
			catch
			{
				
			}
			try
			{
				dg.Select(dg.CurrentRowIndex);
			}
			catch
			{}
			try
			{				
				DataRow r;
				for(int i=0;i<medisoft_kp.Items.Count;i++)
				{
					r=mDB.getrowbyid(ds_m.Tables[0],"TEN='"+medisoft_kp.Items[i].ToString()+"'");
					if(r!=null) 
					{
                        if (r["ma"].ToString() == dg[dg.CurrentRowIndex, 2].ToString()) medisoft_kp.SetItemCheckState(i, CheckState.Checked);
                        else medisoft_kp.SetItemCheckState(i, CheckState.Unchecked);
					}
				}
			}
			catch
			{
			
			}
		}

		private void butCapnhat_Click(object sender, System.EventArgs e)
		{
			string s_makp="",sql="";
			for(int i=0;i<medisoft_kp.Items.Count;i++)		
			{
				if(medisoft_kp.GetItemChecked(i)) s_makp+=mDB.getrowbyid(ds_m.Tables[0],"TEN='"+medisoft_kp.Items[i].ToString()+"'")["ma"].ToString()+",";
			}
			s_makp=s_makp.Trim(',');
			string []arr;
			arr=s_makp.Split(',');
			if(s_makp=="")
			{
				MessageBox.Show(lan.Change_language_MessageText("Chọn khoa phòng!"));
				return;
			}
			if(arr.Length>1)
			{
				MessageBox.Show(lan.Change_language_MessageText("Mỗi khoa phòng bên kế toán chỉ ứng với một khoa phòng bên Medisoft!"));
				return;
			}
			else 
            {
				sql=" update "+user+".bophan set mabaocao="+arr[0].ToString()+" where madv='"+dg[dg.CurrentRowIndex,0].ToString()+"'";
				mDB.Execute_data(sql);
				CurrencyManager cm=(CurrencyManager)dg.BindingContext[dg.DataSource,dg.DataMember];
				DataView dv=(DataView)cm.List;
				dv.AllowNew=false;
				dv.AllowDelete=false;
				dv.AllowEdit=false;
				DataRow r=dv.Table.Select("madv='"+dg[dg.CurrentRowIndex,0]+"'")[0];
				DataSet ds=mDB.get_data("select * from "+user+".bophan where madv='"+dg[dg.CurrentRowIndex,0].ToString()+"'");
				for(int i=0;i<ds.Tables[0].Columns.Count;i++)
				{
					r[i] = ds.Tables[0].Rows[0][i].ToString();
				}
			}
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			DataSet ds=new DataSet();
			ds.Tables.Add("Table");
			ds.Tables[0].Columns.Add("c01");
			DataRow r= ds.Tables[0].NewRow();
			r["c01"]="1";
			ds.Tables[0].Rows.Add(r);
			ds.WriteXml("xml_anhxakp.xml",XmlWriteMode.WriteSchema);
			this.Close();
		}

		private void medisoft_kp_SelectedIndexChanged(object sender, System.EventArgs e)
		{			
			for(int i=0;i<medisoft_kp.Items.Count;i++)
			{				
				medisoft_kp.SetItemCheckState(i,CheckState.Unchecked);
			}
			medisoft_kp.SetItemCheckState(medisoft_kp.SelectedIndex,CheckState.Checked);			
			string s_makp="",sql="";
			for(int i=0;i<medisoft_kp.Items.Count;i++)		
			{
				if(medisoft_kp.GetItemChecked(i)) s_makp+=mDB.getrowbyid(ds_m.Tables[0],"TEN='"+medisoft_kp.Items[i].ToString()+"'")["ma"].ToString()+",";
			}
			s_makp=s_makp.Trim(',');
			string []arr;
			arr=s_makp.Split(',');
			
			if(s_makp=="") s_makp="0";
			else s_makp=arr[0].ToString();			
			sql="update "+user+".bophan set mabaocao="+s_makp+" where madv='"+dg[dg.CurrentRowIndex,0].ToString()+"'";
            mDB.Execute_data(sql);
			CurrencyManager cm=(CurrencyManager)dg.BindingContext[dg.DataSource,dg.DataMember];
			DataView dv=(DataView)cm.List;
			dv.AllowNew=false;
			dv.AllowDelete=false;
			dv.AllowEdit=false;
			DataRow r=dv.Table.Select("madv='"+dg[dg.CurrentRowIndex,0]+"'")[0];
			DataSet ds=mDB.get_data("select * from "+user+".bophan where madv='"+dg[dg.CurrentRowIndex,0].ToString()+"'");
			for(int i=0;i<ds.Tables[0].Columns.Count;i++)
			{
				r[i] = ds.Tables[0].Rows[0][i].ToString();
			}			
		}
	}
}
